﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Company
    {
        public Company() { }
        public int Add(Model.Company model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@ComName", SqlDbType.NVarChar,100),
					new SqlParameter("@ComAddr", SqlDbType.NVarChar,150),
					new SqlParameter("@Tel", SqlDbType.NVarChar,50),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Url", SqlDbType.NVarChar,50),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@Fax", SqlDbType.NVarChar,50),
					new SqlParameter("@EMail", SqlDbType.NVarChar,50),
					new SqlParameter("@BankName", SqlDbType.NVarChar,150),
					new SqlParameter("@BankAccount", SqlDbType.NVarChar,150)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ComName;
            parameters[2].Value = model.ComAddr;
            parameters[3].Value = model.Tel;
            parameters[4].Value = model.UserName;
            parameters[5].Value = model.Url;
            parameters[6].Value = model.Marks;
            parameters[7].Value = model.Fax;
            parameters[8].Value = model.EMail;
            parameters[9].Value = model.BankName;
            parameters[10].Value = model.BankAccount;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Company_ADD",parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Company model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@ComName", SqlDbType.NVarChar,100),
					new SqlParameter("@ComAddr", SqlDbType.NVarChar,150),
					new SqlParameter("@Tel", SqlDbType.NVarChar,50),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Url", SqlDbType.NVarChar,50),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@Fax", SqlDbType.NVarChar,50),
					new SqlParameter("@EMail", SqlDbType.NVarChar,50),
					new SqlParameter("@BankName", SqlDbType.NVarChar,150),
					new SqlParameter("@BankAccount", SqlDbType.NVarChar,150)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.ComName;
            parameters[2].Value = model.ComAddr;
            parameters[3].Value = model.Tel;
            parameters[4].Value = model.UserName;
            parameters[5].Value = model.Url;
            parameters[6].Value = model.Marks;
            parameters[7].Value = model.Fax;
            parameters[8].Value = model.EMail;
            parameters[9].Value = model.BankName;
            parameters[10].Value = model.BankAccount;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Company_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM Company ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }
        public Model.Company GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Company model = new Model.Company();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Company_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                model.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                model.EMail = ds.Tables[0].Rows[0]["EMail"].ToString();

                model.BankName = ds.Tables[0].Rows[0]["BankName"].ToString();
                model.BankAccount = ds.Tables[0].Rows[0]["BankAccount"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
